package com.alstudio.utils.android.media;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Vibrator;

import com.alstudio.utils.log.ALLog;
import com.alstudio.utils.notification.SystemNotificationEnvConfig.SystemSettingsState;

public class ALMediaManager {

	// 震动管理器
	private Vibrator mVibrator;
	// 此实例仅供播放铃声使用
	private MediaPlayer mediaPlayer = null;
	private Context ctx;
	private int defaultRing = 0;
	private AudioManager audioManager = null;

	public ALMediaManager(Context ctx) {
		// TODO Auto-generated constructor stub
		this.ctx = ctx;
		mVibrator = (Vibrator) ctx.getSystemService(Context.VIBRATOR_SERVICE);
		audioManager = (AudioManager) ctx
				.getSystemService(Context.AUDIO_SERVICE);
	}

	/**
	 * 重置为听筒,仅供voip通话使用
	 */
	public void resetSpeaker() {
		audioManager.setMode(AudioManager.MODE_IN_CALL);
	}

	/**
	 * 切换喇叭和听筒
	 * 
	 * @param isOn
	 *            true 设置为喇叭模式 false 为听筒
	 */
	public void setSpeakerState(boolean isOn) {
		audioManager.setSpeakerphoneOn(isOn);
	}

	/**
	 * 检测喇叭是否开启
	 * 
	 * @return
	 */
	public boolean isSpeakerOn() {
		return audioManager.isSpeakerphoneOn();
	}

	/**
	 * 设置通话音量为最大
	 */
	public void setMaxVoiceVolume() {
		int max = audioManager
				.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
		int cur = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
		ALLog.d("最高音量 " + max + " 当前音量  " + cur);
		if (max != cur) {
			audioManager
					.setStreamVolume(AudioManager.STREAM_VOICE_CALL, max, 0);
		}
	}
	
	public void setMute(boolean isMute){
		audioManager.setMode(AudioManager.MODE_NORMAL);
		audioManager.setMicrophoneMute(isMute);
	}
	
	public boolean isMuted(){
		return audioManager.isMicrophoneMute();
	}

	public void setDefaultRing(int ring) {
		this.defaultRing = ring;
	}

	public int getDefaultRing() {
		return defaultRing;
	}

	public void stopPlay() {
		releaseMediaPlayer();
		mVibrator.cancel();
	}

	public void playMusic(Context context, String path) {
		releaseMediaPlayer();
	}

	public void playRing(Uri uri, boolean looping) {
		releaseMediaPlayer();
		mediaPlayer = MediaPlayer.create(ctx, uri);
		mediaPlayer.setLooping(looping);
		if (mediaPlayer != null) {

			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					// 播放已完成
					releaseMediaPlayer();
				}
			});

			mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub
					// 播放失败
					releaseMediaPlayer();

					return false;
				}
			});
			mediaPlayer.start();
		}
	}

	/**
	 * 播放铃声
	 * 
	 * @param ring
	 *            铃声资源
	 */
	private void playRing() {
		releaseMediaPlayer();
		mediaPlayer = MediaPlayer.create(ctx, defaultRing);

		if (mediaPlayer != null) {
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub

					releaseMediaPlayer();

				}
			});

			mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub

					releaseMediaPlayer();

					return false;
				}
			});
			mediaPlayer.start();
		}
	}

	public void playRing(int ring, boolean looping) {
		releaseMediaPlayer();
		mediaPlayer = MediaPlayer.create(ctx, ring);
		mediaPlayer.setLooping(looping);

		if (mediaPlayer != null) {
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub

					// 播放完成后将播放器释放并将将当前播放的语音表情的message id置为空
					releaseMediaPlayer();

				}
			});

			mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub

					// 播放完成后将播放器释放并将将当前播放的语音表情的message id置为空
					releaseMediaPlayer();

					return false;
				}
			});
			mediaPlayer.start();
		}

	}

	/**
	 * 播放铃声
	 * 
	 * @param ring
	 *            铃声资源
	 */
	public void playRing(int ring) {

		releaseMediaPlayer();
		mediaPlayer = MediaPlayer.create(ctx, ring);

		if (mediaPlayer != null) {
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub

					releaseMediaPlayer();

				}
			});

			mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub

					releaseMediaPlayer();
					return false;
				}
			});
			mediaPlayer.start();
		}
	}

	private void playVibrate() {
		long[] vibrate = { 300, 100, 300, 100 };
		mVibrator.vibrate(vibrate, -1);
	}

	private void playVibrate(boolean repeat) {
		long[] vibrate = { 300, 100, 300, 100 };
		int repeatCount = -1;
		if (repeat) {
			repeatCount = 0;
		}
		mVibrator.vibrate(vibrate, repeatCount);
	}

	private void playVibrate(long[] vibrate) {
		mVibrator.vibrate(vibrate, -1);
	}

	public void playRingAndVibrate(SystemSettingsState state, int ring) {
		switch (state) {
		case SOUND_AND_VIBRATION_ALL_ON:
			playRing(ring);
			playVibrate();
			ALLog.d("ring and vibrate all on");
			break;
		case SOUND_ONLY:
			playRing(ring);
			ALLog.d("ring only");
			break;
		case VIBRATION_ONLY:
			playVibrate();
			ALLog.d("vibrate only");
			break;
		case MUTE_TIME:
			ALLog.d("mute time...");
			break;
		case SOUND_AND_VIBRATION_ALL_OFF:
			ALLog.d("ring and vibrate all off");
			break;
		default:
			break;
		}
	}

	public void playRingAndVibrate(SystemSettingsState state, Uri ring,
			boolean looping) {
		switch (state) {
		case SOUND_AND_VIBRATION_ALL_ON:
			playRing(ring, looping);
			playVibrate(looping);
			ALLog.d("ring and vibrate all on");
			break;
		case SOUND_ONLY:
			playRing(ring, looping);
			ALLog.d("ring only");
			break;
		case VIBRATION_ONLY:
			playVibrate(looping);
			ALLog.d("vibrate only");
			break;
		case MUTE_TIME:
			ALLog.d("mute time...");
			break;
		case SOUND_AND_VIBRATION_ALL_OFF:
			ALLog.d("ring and vibrate all off");
			break;
		default:
			break;
		}
	}

	public void playRingAndVibrate(SystemSettingsState state, int ring,
			boolean looping) {
		switch (state) {
		case SOUND_AND_VIBRATION_ALL_ON:
			playRing(ring, looping);
			playVibrate(looping);
			ALLog.d("ring and vibrate all on");
			break;
		case SOUND_ONLY:
			playRing(ring, looping);
			ALLog.d("ring only");
			break;
		case VIBRATION_ONLY:
			playVibrate(looping);
			ALLog.d("vibrate only");
			break;
		case MUTE_TIME:
			ALLog.d("mute time...");
			break;
		case SOUND_AND_VIBRATION_ALL_OFF:
			ALLog.d("ring and vibrate all off");
			break;
		default:
			break;
		}
	}

	public void playRingAndVibrate(SystemSettingsState state) {
		switch (state) {
		case SOUND_AND_VIBRATION_ALL_ON:
			playRing();
			playVibrate();
			ALLog.d("ring and vibrate all on");
			break;
		case SOUND_ONLY:
			playRing();
			ALLog.d("ring only");
			break;
		case VIBRATION_ONLY:
			playVibrate();
			ALLog.d("vibrate only");
			break;
		case MUTE_TIME:
			ALLog.d("mute time...");
			break;
		case SOUND_AND_VIBRATION_ALL_OFF:
			ALLog.d("ring and vibrate all off");
			break;
		default:
			break;
		}
	}

	public void playRingAndVibrate(SystemSettingsState state, int ring,
			long[] vibrate) {
		switch (state) {
		case SOUND_AND_VIBRATION_ALL_ON:
			playRing(ring);
			playVibrate(vibrate);
			ALLog.d("ring and vibrate all on");
			break;
		case SOUND_ONLY:
			playRing(ring);
			ALLog.d("ring only");
			break;
		case VIBRATION_ONLY:
			playVibrate(vibrate);
			ALLog.d("vibrate only");
			break;
		case MUTE_TIME:
			ALLog.d("mute time...");
			break;
		case SOUND_AND_VIBRATION_ALL_OFF:
			ALLog.d("ring and vibrate all off");
			break;
		default:
			break;
		}
	}

	private void releaseMediaPlayer() {
		// synchronized (mediaPlayer)
		{
			if (mediaPlayer != null && mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
				mediaPlayer.release();
				mediaPlayer = null;
			}
		}
	}
}
